<?php
namespace Admin\Model;
use Think\Model;
//用户模型
class MemberModel extends Model{
    //注册自动验证
    protected $_validate = array(
        array('username','require','用户名不能为空'), 
        array('nickname','require','昵称不能为空'), 
        array('phone','require','手机号不能为空'), 
        array('email','require','邮箱不能为空'), 
        array('wechat','require','微信号不能为空'), 
        array('alipay','require','支付宝帐号不能为空'), 
        array('password','require','密码不能为空'),
        array('repassword','require','确认密码不能为空'), 
        array('username', 'checklength', '用户名长度必须在6-15位之间！', 0, 'callback', 3, array(6, 15)), 
        array('username','','用户名称已经存在！',0,'unique',1), 
        array('phone','','手机号已经存在！',0,'unique',1), 
        array('email', '/^[\w\-\.]+@[\w\-\.]+(\.\w+)+$/', '邮箱格式错误！', 2, 'regex'),
        array('email','','邮箱已经存在！',0,'unique',1), 
        array('wechat','','微信号已经存在！',0,'unique',1), 
        array('alipay','','支付宝账号已经存在！',0,'unique',1), 
        array('repassword','password','两次密码不一致。',0,'confirm'), 
    );
    //自动完成
    protected $_auto = array ( 
         array('password','md5',3,'function') , 
         array('create_time','time',1,'function'), 
     );

    //获取代理号
    public function set_agentid(){
        //获取当前最大的代理号
        $agentid = $this->max('agentid');
        $agentid = $agentid+1;
        return $agentid;
    }
    //验证用户名长度
    public  function checklength($str, $min, $max) {
        preg_match_all("/./u", $str, $matches);
        $len = count($matches[0]);
        if ($len < $min || $len > $max) {
            return false;
        } else {
            return true;
        }
    }
    //新增用户
    public function add_member($pagentid){
        //新增用户为默认普通用户未成为代理
        $data = $this->create();
        $data['group_id'] = 9;
        $data['agentid'] = $this->set_agentid();
        //获取推荐人信息
        $member = $this->where(array('agentid'=>$pagentid))->find();
        //如果没有推荐人
        if($member['id'] == 1){
            $data['p1'] = 1;
            $data['p2'] = 0;
        }else{
            $data['p1'] = $member['id'];
            //获取第二级
            $member2 = $this->find($member['p1']);
            $data['p2'] = $member2['id'];
        }
        if($this->add($data)){
            return true;
        }else{
            $this->error = $this->getError();
            return false;
        }
    }
	//登录验证
    public function check_login($data){
		$data['password'] = md5($data['password']);
		$user = $this->where(array('username'=>$data['username']))->find();
		if(!$user){$this->error='用户名不存在';return false;}
		if($user['disable'] == 1){$this->error='账号被禁用';return false;}
		if($user['password'] != $data['password']){$this->error='密码错误';return false;}
		$user = array('userid'=>$user['id'],'username'=>$user['username']);
		session('user',$user);
		return true;
	}

	//获取用户
    public function get_member($id='',$agentid='',$where='',$page='',$order=''){
        if($id){
            $member = $this->find($id);
            return $member;
        }
        if($agentid){
            $member = $this->where(array('agentid'=>$agentid))->find();
            return $member;
        }
        if($where){
            $count = $this->where($where)->count();
            $page = new \Think\Page($count,$page);
            $member = $this->where($where)->limit($page->firstRow.','.$page->listRows)->order($order)->select();
            $data = [
                'mmeber'    =>  $member,
                '_page'     =>  $page->show(),
            ];
            return $data;
        }
    }

}
